/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entity.Address;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import util.DatabaseUtil;

/**
 *
 * @author zhang
 */
public class AddressDao {
    public List<Address> getAllAddress(int memberId){   //获取用户所有地址
        List<Address> list=new ArrayList<>();
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("select * from deliveryaddress where memberid=?");
            ps.setInt(1,memberId);
            rs=ps.executeQuery();
            while(rs.next()){
                list.add(new Address(rs.getInt(1),rs.getString(3),rs.getString(4),rs.getString(5),rs.getInt(6),rs.getBoolean(7)));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps, rs);
        }
        return list;
    }
    
    public List<Address> getAllUndeletedAddress(int memberId){   //获取用户未被删除的地址
        List<Address> list=new ArrayList<>();
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("select * from deliveryaddress where memberid=? and isdelete=0");
            ps.setInt(1,memberId);
            rs=ps.executeQuery();
            while(rs.next()){
                list.add(new Address(rs.getInt(1),rs.getString(3),rs.getString(4),rs.getString(5),rs.getInt(6),rs.getBoolean(7)));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps, rs);
        }
        return list;
    }
    
    
    
    public Address getAddressById(int id){
        Address address=null;
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("select * from deliveryaddress where id=?");
            ps.setInt(1,id);
            rs=ps.executeQuery();
            while(rs.next()){
                address=new Address(rs.getInt(1),rs.getString(3),rs.getString(4),rs.getString(5),rs.getInt(6),rs.getBoolean(7));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps, rs);
        }
        return address;
    }
    
    
    public Boolean addAddress(int memberId,String address,String addressee,String phone,int zipcode){
        int i=0;
        Connection con=null;
        PreparedStatement ps=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("insert into deliveryaddress(memberid,address,addressee,phone,zipcode) values(?,?,?,?,?)");
            ps.setInt(1,memberId);
            ps.setString(2,address);
            ps.setString(3,addressee);
            ps.setString(4,phone);
            ps.setInt(5,zipcode);
            i=ps.executeUpdate();
            if(i!=0){
                return true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps,null);
        }
        return false;
    }
    
    public Boolean deleteAddress(int id){
        int i=0;
        Connection con=null;
        PreparedStatement ps=null;
        try{
            con=DatabaseUtil.connect();
            ps=con.prepareStatement("update deliveryaddress set isdelete=1 where id=?");
            ps.setInt(1,id);
            i=ps.executeUpdate();
            if(i!=0){
                return true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DatabaseUtil.closeConnection(con, ps,null);
        }
        return false;
    }
}
